﻿/*
===============================================================================
*文件名称(File Name) ServiceBusConfig
*功能描述(Description):
*数据表(Tables):
*作者(Author): 赵高磊
*日期(Create Date):11/30/2015 3:59:36 PM
*修改记录(Revision History):
* V1.0.0.0 Created by 赵高磊 at 11/30/2015 3:59:36 PM
===============================================================================
*/

using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;

namespace ServiceBus.Core
{
    /// <summary>
    /// 承载Sb的配置信息
    /// </summary>
    public class ServiceBusConfig
    {
        /// <summary>
        /// Gets the HTTP port.
        /// </summary>
        /// <value>The HTTP port.</value>
        public static int HttpPort
        {
            get
            {
                return ConfigHelper.GetValue("HttpPort") == null ?
                    9355
                    : int.Parse(ConfigHelper.GetValue("HttpPort"));
            }
        }

        /// <summary>
        /// Gets the server FQDN.
        /// </summary>
        /// <value>The server FQDN.</value>
        public static string ServerFqdn
        {
            get
            {
                return string.IsNullOrWhiteSpace(ConfigHelper.GetValue("ServerFQDN")) ?
                    System.Net.Dns.GetHostEntry(string.Empty).HostName
                    : ConfigHelper.GetValue("ServerFQDN");
            }
        }

        /// <summary>
        /// Gets the service namespace.
        /// </summary>
        /// <value>The service namespace.</value>
        public static string ServiceNamespace
        {
            get
            {
                return string.IsNullOrWhiteSpace(ConfigHelper.GetValue("ServiceNamespace")) ?
                    "ServiceBusDefaultNamespace"
                    : ConfigHelper.GetValue("ServiceNamespace");
            }
        }

        /// <summary>
        /// Gets the TCP port.
        /// </summary>
        /// <value>The TCP port.</value>
        public static int TcpPort
        {
            get
            {
                return ConfigHelper.GetValue("TcpPort") == null ?
                    9354 : int.Parse(ConfigHelper.GetValue("TcpPort"));
            }
        }

        public static MessagingFactory InitializateMsgFactory()
        {
            return MessagingFactory.CreateFromConnectionString(GetConnectBuilder().ToString());
        }

        public static NamespaceManager InitializateNpManager()
        {
            return NamespaceManager.CreateFromConnectionString(GetConnectBuilder().ToString());
        }

        private static ServiceBusConnectionStringBuilder GetConnectBuilder()
        {
            var connectBuilder = new ServiceBusConnectionStringBuilder
            {
                ManagementPort = HttpPort,
                RuntimePort = TcpPort
            };
            connectBuilder.Endpoints.Add(new
                UriBuilder
            {
                Scheme = "sb",
                Host = ServerFqdn,
                Path = ServiceNamespace
            }.Uri);
            connectBuilder.StsEndpoints.Add(
                new UriBuilder
                {
                    Scheme = "https",
                    Host = ServerFqdn,
                    Port = HttpPort,
                    Path = ServiceNamespace
                }.Uri);
            return connectBuilder;
        }
    }
}